<?php

/**
 * 将当前进程变成守护进程(Daemon)的类
 */
class PHPShouhu {
    /**
     * 变成守护进程
     */
    public function transform() {
        $pid = pcntl_fork();
        if($pid == -1) {
            throw new Exception("Cannot fork");
        } else if($pid) {// parent
            exit(0);
        }

        // detatch from the controlling terminal
        if (posix_setsid() == -1) {
            throw new Exception("Could not detach from terminal");
        }
    }
}
?>